<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 活动接口
 */
class Activity extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 活动列表
     *
     */
    public function activity_list()
    {
        if (\request()->isGet())
        {

            $status = (int)$this->request->get('status',0);
            $page = (int)$this->request->get('page');

            $page = $page <1 ? 1 : $page;
            $limit = 10;

            $where = [];
            if(!empty($status))
            {
                if($status = 1)
                {
                    $where['begin_time'] = ['<',time()];
                    $where['end_time'] = ['>',time()];
                }

                if($status = 2)
                {
                    $where['end_time'] = ['<',time()];
                }

                if($status = 3)
                {
                    $where['time'] = ['<',time()];
                }
            }

            $offset = ($page - 1) * $limit;
            $list = Model('activity')->where($where)->order('id desc')->limit($offset,$limit)->select();
            $total = Model('activity')->where($where)->order('id desc')->limit($offset,$limit)->count();

            if(!empty($list))
            {
                foreach($list as $key => $value)
                {
                    if($value['begin_time'] < time() && $value['end_time'] > time())
                    {
                        $list[$key]['status'] = '进行中';
                    }
                    if($value['end_time'] <= time())
                    {
                        $list[$key]['status'] = '已截止';
                    }
                    if($value['time'] <= time())
                    {
                        $list[$key]['status'] = '已结束';
                    }
                    $list[$key]['created_time'] = date('Y-m-d H:i',$value['created_time']);
                    $list[$key]['updated_time'] = date('Y-m-d H:i',$value['updated_time']);
                }
            }

            $data = [
                'total' => $total,
                'per_page' => $limit,
                'current_page' => $page,
                'last_page' => ceil($total/$limit),
                'data' => $list
            ];
            $this->success("ok", $data);
        }
    }

    /**
     * 活动详情
     *
     */
    public function activity_detail()
    {
        if (\request()->isGet())
        {

            $id = (int)$this->request->get('id',0);

            $activity = Model('activity')->find($id);

            if(!empty($activity))
            {
                if($activity['begin_time'] < time() && $activity['end_time'] > time())
                {
                    $activity['status'] = '进行中';
                }
                if($activity['end_time'] <= time())
                {
                    $activity['status'] = '已截止';
                }
                if($activity['time'] <= time())
                {
                    $activity['status'] = '已结束';
                }

                $activity['created_time'] = date('Y-m-d H:i',$activity['created_time']);
                $activity['updated_time'] = date('Y-m-d H:i',$activity['updated_time']);
            }
            $this->success("ok", $activity);
        }
    }
}